﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using nwrole.toolbox.sync;
using sync_test.maps;
using nwrole.toolbox.sync.contract;
using System.ServiceModel;
using BLToolkit.Data.DataProvider;
using sync_test.util;
using nwrole.toolbox.sync.entity;
using sync_test.action;
using nwrole.toolbox.sync.ServiceHost;
using CodeProject;

namespace sync_test
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                foreach (var f in FastDirectoryEnumerator.EnumerateFiles("d:/", "*.*", System.IO.SearchOption.AllDirectories))
                {
                    System.Console.WriteLine(f.Name);
                }

                // TODO:Chanan
                // denna måste sätta true
                bool chanan = true;

                // start service host
         /*       if (1 == 1)
                {
                    string destinationDbConStr = ConfigurationManager.ConnectionStrings["SyncDestinationDb"].ConnectionString;

                    SyncDestinationServiceHost host = new SyncDestinationServiceHost();
                    host.Open("http://localhost:9004/", "hejsan.svc", destinationDbConStr);// TODO: set connectionstring 120924, previously taken from ConnectionUtil
                }
                */

                List<SyncEntity> maps = null;
                // load maps from db
                if (1 == 1)
                {
                    if (chanan)
                    {
                        // TODO:Chanan
                        // den här koden är inte testad
                        // 1. du måste skapa tabeller
                        // 2. du måste fylla tabellerna med data
                        // 3. du måste provköra koden
                        var sync = GetDateTimeTableSync(0);
                        SyncEntityRepository dal = new SyncEntityRepository();
                        // dal.ConnectionString = sync.SourceConnectionString;
                        dal.DataProvider = new SqlDataProvider();
                        maps = dal.FindAllToSync();
                    }
                    else
                    {
                        // maps = LoadSyncMaps.Run(GetGhostTableSync(0));
                    }
                }

               // SyncEntity e = maps.Where(a=> a.ShadowTableName == "KUNDAVIADRESS_ghost").First();
               // DeployInt.Run(e, GetGhostTableSync(0));


                // GetTableToDisk
                if (1 == 2)
                {
                 //   TableToDisk ttd = GetTableToDisk();
                 //   ttd.run();
                    return; 
                }

     /*           // SaveSyncToDb
                if (1 == 2)
                {
                    SaveSyncToDb.Run(GetGhostTableSync(0));
                    return;
                } */

                // deploy
                if (1 == 2)
                {
                    if (chanan)
                    {
                        ChananDeploySync.Run(maps, GetDateTimeTableSync(0));
                    }
                    else
                    {
                        // DeploySync.Run(maps, GetGhostTableSync(0));
                    }
                }

               //  syncTexter(1000, TEXTER.Mapping);

                if (1 == 1)
                {
                    Console.WriteLine("Press enter to start");
                    Console.ReadLine();

                    EndpointAddress ep = new EndpointAddress("http://localhost:9004/hejsan.svc");
                    ISyncDestinationService client = ChannelFactory<ISyncDestinationService>.CreateChannel(new BasicHttpBinding(), ep);
                    //client.ProcessMessage(null);

                    // Console.ReadLine();

                    // ITestService client = InitializeWS<ITestService>();
                    if (chanan)
                    {
                        // TODO:Chanan
                        // detta måste provköras
                        ChananSync.Run(maps, GetDateTimeTableSync(1000), client);
                    }
                    else
                    {
                        // Sync.Run(maps, GetGhostTableSync(1000), client);
                    }
                }

                Console.WriteLine("Done!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }

        private static T InitializeWS<T>()
        {
            EndpointAddress ep = new EndpointAddress("http://localhost:4781/TestService.svc");
            T proxy = ChannelFactory<T>.CreateChannel(new BasicHttpBinding(), ep);
            return proxy;
        }
/*
        private static SyncResult syncTexter(int batchSize, SyncMap map)
        {
            try
            {
                // setup sync
                var client = InitializeWS<ITestService>();
                GhostTableSync sync = GetGhostTableSync(batchSize);
                // sync texter
                var result = sync.SyncDirectUpdate(map, (bytes, tableName, schema) =>
                {
                    Console.Write(".");
                    MessageEnvelope msg = new MessageEnvelope(tableName, "ibbe3", bytes, schema);
                    client.ProcessMessage(msg);
                    return 1;
                });
                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                int a = 1;
            }
            return null;
        }
        */



      /*  public static SyncEntity CreateSyncMap(string tableName)
        {
            try
            {
                System.Console.Write("Creating SyncMap " + tableName + " ....");
                // setup sync
                GhostTableSync sync = GetGhostTableSync(0);
                var result = sync.CreateSyncMap("WF", tableName);
                System.Console.WriteLine("Done.");
                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return null;
        }*/

                            // DeployDestinationInt(HYRESDEL.Mapping);
                    // DeployDestinationInt(OBJEKTSBUNDET.Mapping);


        //public static void DeployImport(SyncEntity map)
        //{
        //    try
        //    {
        //        System.Console.Write("Deploying " + map.Name + " ....");
        //        DeployImportInt(map);
        //        System.Console.WriteLine("Done.");
        //    }
        //    catch (Exception e)
        //    {
        //        Console.WriteLine(e.ToString());
        //        int a = 1;
        //    }
        //}


        private static DateTimeTableSync GetDateTimeTableSync(int batchSize)
        {
            DateTimeTableSync sync = new DateTimeTableSync();
            sync.BatchSize = batchSize;
            sync.SourceConnectionString = SyncSettings.SrcConnectionString;
            sync.DestinationConnectionString = SyncSettings.DestConnectionString;
            sync.sourceDataProvider = new SqlDataProvider();
            sync.destinationDataProvider = new SqlDataProvider();
            sync.sqlBuilder = new SqlBuilder();
            return sync;
        }

        
   /*     private static GhostTableSync GetGhostTableSync(int batchSize)
        {
            GhostTableSync sync = new GhostTableSync();
            sync.BatchSize = batchSize;
            sync.SourceConnectionString = SyncSettings.SrcConnectionString;
            sync.DestinationConnectionString = SyncSettings.DestConnectionString;
            sync.sourceDataProvider = new OdpDataProvider();
            sync.destinationDataProvider = new SqlDataProvider();
            sync.sqlBuilder = new SqlBuilder();
            return sync;
        } */

     /*   private static TableToDisk GetTableToDisk()
        {
            TableToDisk sync = new TableToDisk();
            sync.SourceConnectionString = SyncSettings.SrcConnectionString;
            sync.DestinationConnectionString = SyncSettings.DestConnectionString;
            sync.sourceDataProvider = new OdpDataProvider();
            sync.destinationDataProvider = new SqlDataProvider();
            sync.sqlBuilder = new SqlBuilder();
            return sync;
        } */

    }
}




// syncTable(1000, HYRESDEL);
// syncTable(1000, OBJEKTSBUNDET.Mapping);

// PriceSync ps = new PriceSync();
// ps.Run2();
// ps.DeployDestinationInt();